﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragUI : MonoBehaviour, IDragHandler, IPointerDownHandler
{
    private Vector2 offsetPos;  //临时记录点击点与UI的相对位置

    public void OnDrag(PointerEventData eventData)
    {
        if (eventData.position.x > 1500)
        {
            eventData.position = new Vector3(1500, eventData.position.y);
        }
        if (eventData.position.x < 300)
        {
            eventData.position = new Vector3(300, eventData.position.y);
        }
        if (eventData.position.y > 1050)
        {
            eventData.position = new Vector3(eventData.position.x, 1050);
        }
        if (eventData.position.y < 0)
        {
            eventData.position = new Vector3(eventData.position.x, 0);
        }
        transform.position = eventData.position - offsetPos;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (PlayerPrefs.GetInt("CreateRelation") == 1)
        {
            if (PlayerPrefs.GetInt("IsCreatingRelation") == 1)
            {
                PlayerPrefs.SetInt("IsCreatingRelation", 0);
                PlayerPrefs.SetInt("CreateRelation", 0);
                GameObject.Find("GameManager").GetComponent<TongXunLuManager>().CreateNewRelation(this.gameObject);
            }
            else
            {
                GameObject.Find("GameManager").GetComponent<TongXunLuManager>().This = this.gameObject;
                PlayerPrefs.SetInt("IsCreatingRelation", 1);
            }
        }

        offsetPos = eventData.position - (Vector2)transform.position;
    }
}